package ui;

import java.io.IOException;

import javax.swing.JOptionPane;

import dao.TeacherDao;
import dao.TeacherDaoJDBCImpl;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author qiaopeng
 */
public class SearchBycourseFrame extends javax.swing.JFrame {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/**
     * Creates new form NewJFrame1
     */
    public SearchBycourseFrame(String username) {
        initComponents(username);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents(String username) {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                
            },
            new String [] {
                
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("确定");
        getRootPane().setDefaultButton(jButton1);//设置回车响应
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt,username);
            }
        });

        jLabel1.setText("输入科目名称：");

        jTextField1.setText("");

        jButton2.setText("成绩图形分析");
        
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("导出");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                try {
					jButton3ActionPerformed(evt);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(111, 111, 111)
                .addComponent(jLabel1)
                .addGap(41, 41, 41)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 166, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 250, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(148, 148, 148))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton2)
                .addGap(85, 85, 85)
                .addComponent(jButton3)
                .addGap(72, 72, 72))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 466, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton3))
                .addGap(30, 30, 30))
        );

        pack();
    }// </editor-fold>                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt,String username) {                                         
    	String str = jTextField1.getText();
        TeacherDao th = new TeacherDaoJDBCImpl();
        if(th.checkhavecourse(username, str)==true) {
        	TeacherFrame.stu = th.getsourcebyname(str);
        	Object [][] object = new Object [TeacherFrame.stu.size()][5];
        	String [] s = {"学号","姓名","班级","科目","成绩"};
        	for(int i =0 ;i < TeacherFrame.stu.size();i++) {
        		object[i][0] = TeacherFrame.stu.get(i).getStudentID();
        		object[i][1] = TeacherFrame.stu.get(i).getName();
        		object[i][2] = TeacherFrame.stu.get(i).getClasses();
        		object[i][3] = TeacherFrame.stu.get(i).getSublist().get(0).getName();
        		object[i][4] = TeacherFrame.stu.get(i).getSublist().get(0).getScore();
        	}
        	jTable1.setModel(new javax.swing.table.DefaultTableModel(
                    object,
                    s
                ));
           jScrollPane1.setViewportView(jTable1);
        }
        else {
        	JOptionPane.showMessageDialog(null,"您没有教授这门课");
        }
    }                                        

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    	TeacherDao th = new TeacherDaoJDBCImpl();
    	boolean check = th.draw(TeacherFrame.stu);
    	if(check==false) {
    		JOptionPane.showMessageDialog(null,"无法作图");
    	}
    }                                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) throws IOException{                                         
    	try {
	    	TeacherDao th = new TeacherDaoJDBCImpl();
	    	th.ExportBycoursePOI(TeacherFrame.stu);
    	}catch(IOException e){
    		e.printStackTrace();
    	}
    }                                        

    

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration                   
}
